﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Models;

namespace VietPremier.Controllers
{
    public class DestinationController : Controller
    {
        //
        // GET: /Destination/

        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityDestinationAreas;
                return View(query.ToList());
            }
        }

        public ActionResult DestinationInfo()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityDestinationInfos;
                return PartialView(query.FirstOrDefault());
            }
        }

        public ActionResult DestinationArea(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == id);
                return PartialView(query);
            }
        }

        public ActionResult DestinationCountryList(string title) // string Area
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int id = Utilities.GetIdByTitle(title, VietPremierMenu.DestinationArea, VietPremierDetailList.Detail);

                ViewBag.AreaName = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == id).Name;
                ViewBag.IdArea = id;
                var query = from p in db.EntityDestinationCountries
                            join q in db.EntityDestinationAreas on p.IdDestinationArea equals q.Id
                            where p.IdDestinationArea == id
                            select p;
                return View(query.ToList());
            }
        }

        public ActionResult DestinationCountry(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id);
                return PartialView(query);
            }
        }

        public ActionResult Destination(string title, string name)// string country, string area
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int id = Utilities.GetIdByTitle(title, VietPremierMenu.DestinationCountry, VietPremierDetailList.Detail);
                int idArea = Utilities.GetIdByTitle(name, VietPremierMenu.DestinationArea, VietPremierDetailList.Detail);

                ViewBag.IdCountry = id;
                ViewBag.CountryName = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id).Name;
                ViewBag.IdArea = idArea;
                ViewBag.AreaName = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == idArea).Name;
                GetDestinationModel model = new GetDestinationModel();
                foreach (var destination in db.EntityDestinations.Where(p => p.IdDestinationCountry == id))
                {
                    model.listDestination.Add(new destination()
                    {
                        Id=destination.Id,
                        Name=destination.Name,
                        Image=destination.Image,
                        Description = destination.Description.Substring(0, (destination.Description.Length > 150) ? 150 : destination.Description.Length),
                    });
                }

                ViewBag.Titletag = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id).Titletag;
                ViewBag.Meta_description = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id).Meta_description;
                ViewBag.Meta_keyword = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == id).Meta_keyword;
               
                return View(model);
            }
        }

        public ActionResult DestinationDetail(string title, string name, string destination)// string country, string area, string destination
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                int idCountry = Utilities.GetIdByTitle(title, VietPremierMenu.DestinationCountry, VietPremierDetailList.Detail);
                int idArea = Utilities.GetIdByTitle(name, VietPremierMenu.DestinationArea, VietPremierDetailList.Detail);
                int id = Utilities.GetIdByTitle(destination, VietPremierMenu.Destination, VietPremierDetailList.Detail);
                ViewBag.idArea = idArea;
                ViewBag.AreaName = db.EntityDestinationAreas.FirstOrDefault(p => p.Id == idArea).Name;
                ViewBag.idCountry = idCountry;
                ViewBag.CountryName = db.EntityDestinationCountries.FirstOrDefault(p => p.Id == idCountry).Name;
                var query = from p in db.EntityDestinations
                            where p.Id == id
                            select new AdminDestinationDetailModel
                            {
                                Id = p.Id,
                                Name = p.Name,
                                Image = p.Image,
                                Description = p.Description,
                                Body = p.Body,
                                listImage =( from q in db.EntityImageDestinations
                                            where q.IdDestination == id
                                            select new ImageDestination
                                            {
                                                Id = q.Id,
                                                _Image = q.Image

                                            }).ToList()
                            };
                return View(query.FirstOrDefault());
            }
        }

        public ActionResult SameName(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                GetDestinationModel model = new GetDestinationModel();
                string name = db.EntityDestinations.FirstOrDefault(p => p.Id == id).Duration;
                if (name != null)
                {
                    string[] strTemp = name.Split('-');
                    foreach (var item in strTemp)
                    {
                        string itemfix = item.Trim();
                        foreach (var travelCity in db.EntityTravelCities.Where(o => o.Name.Contains(itemfix)))
                        {
                            EntityTravelProduction travelProduct = db.EntityTravelProductions.FirstOrDefault(o => o.Id == travelCity.IdTravelProduction);

                            model.listTravel.Add(new travel()
                            {
                                Id = travelCity.Id,
                                ProductName = travelProduct.Name,
                                Name = travelCity.Name.Substring(0, (travelCity.Name.Length > 38) ? 38 : travelCity.Name.Length),
                                Image = travelCity.Image,
                                Description = travelCity.Description.Substring(0, (travelCity.Description.Length > 147) ? 147 : travelCity.Description.Length)
                            });
                        }
                    }
                    return PartialView(model);

                }
                else
                {
                    return PartialView(model);
                }
                
            }
        }        

        public ActionResult Hotel(int id)
        {
            using(dbVietPremierDataContext db=new dbVietPremierDataContext())
            {
            GetHotelModel model = new GetHotelModel();
            foreach (var hotel in db.EntityTravelHotels.Where(p => p.IdTravelCity == id))
            {
                model.listHotel.Add(new Hotel()
                {
                    Id=hotel.Id,
                    Name=hotel.Name,
                    Address=hotel.Address,
                    Phone=hotel.Phone,
                    Rate=hotel.Rate,
                    Image=hotel.Image
                });
            }
            return PartialView(model);
            }
        }
    }
}
